﻿import mx.utils.Delegate;
import arkadium.EventManager;
import arkadium.AppConfigFile;
import arkadium.EncryptionManager;
import arkadium.XMLFile;
import game.Main;
import game.DataManager;

class game.EndPanel extends arkadium.GUIPanel
{
	private var _sGameEndSubmitUrl:String;
	private var _sbtRestart:Button;
	private var _smcErrorDelay:MovieClip;
	private var _smcGameendContainer:MovieClip;
	

	private function EndPanel()
	{
		super.registerPanel( this, "END_PANEL" );
		_sbtRestart.onRelease = Delegate.create( this, restartGame );
		_smcErrorDelay.stop();
	}
	
	public function onInitialize()
	{	
		Main.getInstance()._cEventManager.addEventListener( "END_GAME", Delegate.create( this, endGame ) ); 
	}

	private function restartGame(){
		_smcGameendContainer.endContainer.removeMovieClip();
		Main.getInstance()._cEventManager.broadcastMessage( "RESTART_GAME" );
		super.showMe( false );
	}
	
	private function endGame(){
		var nScore:Number = DataManager.getInstance().getScore()
		_smcGameendContainer._sdtScore.text = nScore;
		_smcGameendContainer.g_numScore = nScore;
		_level0.g_numScore = nScore;

		
		//GDG game center code
		var sCurrentFramework:String = Main.getInstance()._sFrameworkSetting;
		trace("CHECkTHISOUT"+sCurrentFramework);
		if (sCurrentFramework == "stolen" || sCurrentFramework == "gamecenter"){
			var sGameEndSubmitUrl:String = Main.getInstance()._cAppConfigFile.getData('gameEndSubmitUrl')
			if (sGameEndSubmitUrl == undefined) sGameEndSubmitUrl ='http://freeplay.gamedek.com/gameend.aspx';
			
			//this is a neat little function that sasha wanted to ensure that he could send a 
			//submit url with only one ? 
			
			var sSign:String = '?'
			var sLeaderBoard:String;
			sLeaderBoard = _root.leaderBoardURL;
			var aLeaderBoard:Array = new Array()
			aLeaderBoard =sLeaderBoard.split("")

			for(var i:Number=0;i<aLeaderBoard.length;i++){
				if("?"==aLeaderBoard[i]){
                	sSign='&'
				}
			}
			var cGameEndData:XMLFile = new XMLFile();
			cGameEndData.loadFile( this, onLoadGameEnd, sGameEndSubmitUrl + ''+sSign+'params=' + Main.getInstance()._cEncryptionManager.encryptParams('freePlayID=' + Main.getInstance()._cAppConfigFile.getData('freeplayid') + '&score=' + nScore + '&timeStamp='+Main.getInstance()._cAppConfigFile.getData('timeStamp')) );
		}
		
		else if (sCurrentFramework == "config"){
			
			loadTheChosenGameEnd();
		}

		_smcErrorDelay.gotoAndPlay(1);
		_smcGameendContainer.stop();
		
		super.showMe( true );
	}
	
	function loadTheChosenGameEnd(){
		
		var aEndUrlArray:Array = new Array();
		var sEndUrl:String = Main.getInstance()._cAppConfigFile.getData('endURL')
		trace("it's this"+sEndUrl);
		aEndUrlArray = sEndUrl.split(".");
		var sSubString:String = aEndUrlArray[aEndUrlArray.length - 1];
		if(sSubString == "aspx" || sSubString == "html") getURL(sEndUrl,"_self");
		else{
			
			var mcGameEnd:MovieClip = _smcGameendContainer.createEmptyMovieClip ("endContainer", 100);
			_smcGameendContainer.endContainer.loadMovie(sEndUrl,mcGameEnd);
			_level0.playAgain = _smcGameendContainer.playAgain = Delegate.create( this, restartGame );
			Main.getInstance()._cEventManager.broadcastMessage( "END_LOADED" );
			super.showMe( true );
		}
	}

	function onLoadGameEnd(xXML:XML, bSuccess:Boolean )
	{
		if ( bSuccess ){
			_level0.gameEndUrl = xXML.firstChild.firstChild.attributes.url;
			_level0.affiliateID = xXML.firstChild.firstChild.attributes.affiliateID;
			Main.getInstance()._cEncryptionManager.decryptParams(_level0,xXML.firstChild.firstChild.nodeValue);
			Main.getInstance()._cAppConfigFile.setData("endURL", _level0.gameEndUrl);
			loadTheChosenGameEnd()
		}
	}
}